<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="500px"
    @close="close"
  >
    <el-form ref="formRef" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="上级菜单" prop="title">
        <el-cascader
          v-model="form.upRouteID"
          :options="optionsList"
          :props="optionsProps"
        />
      </el-form-item>
      <el-form-item label="菜单名称" prop="title">
        <el-input v-model="form.title" />
      </el-form-item>
      <el-form-item label="path" prop="path">
        <el-input v-model="form.path" />
      </el-form-item>
      <el-form-item label="name" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="component" prop="component">
        <el-input v-model="form.component" />
      </el-form-item>
      <el-form-item label="icon" prop="icon">
        <el-input v-model="form.icon" />
      </el-form-item>
      <el-form-item label="breadcrumbHidden" prop="breadcrumbHidden">
        <el-switch
          v-model="form.breadcrumbHidden"
          class="mt-2"
          inline-prompt
          :active-value="1"
          :inactive-value="0"
          :active-icon="Check"
          :inactive-icon="Close"
        />
      </el-form-item>
      <el-form-item label="tabHidden" prop="tabHidden">
        <el-switch
          v-model="form.tabHidden"
          class="mt-2"
          inline-prompt
          :active-value="1"
          :inactive-value="0"
          :active-icon="Check"
          :inactive-icon="Close"
        />
      </el-form-item>
      <el-form-item label="hidden" prop="hidden">
        <el-switch
          v-model="form.hidden"
          class="mt-2"
          inline-prompt
          :active-value="1"
          :inactive-value="0"
          :active-icon="Check"
          :inactive-icon="Close"
        />
      </el-form-item>
      <el-form-item label="isSubRoute" prop="isSubRoute">
        <el-switch
          v-model="form.isSubRoute"
          class="mt-2"
          inline-prompt
          :active-value="1"
          :inactive-value="0"
          :active-icon="Check"
          :inactive-icon="Close"
        />
      </el-form-item>
      <el-form-item label="noClosable" prop="noClosable">
        <el-switch
          v-model="form.noClosable"
          class="mt-2"
          inline-prompt
          :active-value="1"
          :inactive-value="0"
          :active-icon="Check"
          :inactive-icon="Close"
        />
      </el-form-item>
      <el-form-item label="dot" prop="dot">
        <el-switch
          v-model="form.dot"
          class="mt-2"
          inline-prompt
          :active-value="1"
          :inactive-value="0"
          :active-icon="Check"
          :inactive-icon="Close"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { doEdit, getOptionsList } from '@/api/systemMenuset'

  export default defineComponent({
    name: 'SystemMenusetEdit',
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        formRef: null,
        optionsList: [],
        listLoading: true,
        form: {
          RouteID: 0,
        },
        rules: {
          title: [{ required: true, trigger: 'blur', message: '请输入标题' }],
        },
        title: '',
        dialogFormVisible: false,
      })
      const optionsProps = {
        checkStrictly: true,
      }
      const showEdit = (row) => {
        if (!row) {
          state.title = '添加'
        } else {
          state.title = '编辑'
          state.form = Object.assign({}, row)
          state.form.RouteID = 0
        }
        state.dialogFormVisible = true
      }
      const getOptions = async () => {
        state.listLoading = true
        const {
          data: { RouterOptions },
        } = await getOptionsList(state.form)
        state.optionsList = RouterOptions
        // console.log(RouterOptions, 'RouterOptions')
        state.listLoading = false
      }
      const close = () => {
        state['formRef'].resetFields()
        state.form = {}
        state.dialogFormVisible = false
      }

      const save = () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            // console.log(state.form, 'state.form')
            const { msg } = await doEdit(state.form)
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            emit(['fetch-data'])
            close()
          }
        })
      }
      onMounted(() => {
        getOptions()
      })
      return {
        ...toRefs(state),
        optionsProps,
        showEdit,
        getOptions,
        close,
        save,
      }
    },
  })
</script>
